/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deprecated;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.math.FastMath;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Spatial;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.scene.shape.Quad;
import com.jme3.texture.Texture;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import tools.RenderToTexture;
import tools.Transparency;

/**
 *
 * @author Jannes
 */
public class RotationTextureTools {
    private static float resolution = 10f;
    
    public static Spatial createRotationTextureBillboard(Spatial model, int textureAngles, RenderManager renderManager,AssetManager assetManager, String name){
        BufferedImage[] textureList = new BufferedImage[textureAngles];
        int maxWidth = 0;        
        for(int i = 0; i < textureAngles; i++){
            float angle = FastMath.TWO_PI*i/textureAngles;
            RenderToTexture rtt = new RenderToTexture(resolution, renderManager,model, angle);
            textureList[i] = rtt.getImageBlueBackGround(); 
            maxWidth = Math.max(maxWidth, textureList[i].getWidth());            
        }
        BufferedImage texture = new BufferedImage(maxWidth*textureAngles,textureList[0].getHeight(),BufferedImage.TYPE_INT_ARGB);
        Graphics g = texture.createGraphics();
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, texture.getWidth(), texture.getHeight());
        for(int i = 0; i < textureAngles; i++){
            BufferedImage img = textureList[i];
            int xOffset = maxWidth*i;
            xOffset += ((maxWidth - img.getWidth())/2);
            g.drawImage(textureList[i],xOffset , 0, null);
        }
        g.dispose();
        texture = Transparency.makeColorTransparent(texture, Color.BLUE);        
        File texFile = new File("assets/Textures/RotationTextures/" + name + ".png");
        texFile.getParentFile().mkdirs();
        try{
            ImageIO.write(texture, "png", texFile);
        }catch(Exception exc){}        
        
        Mesh m = new Quad(maxWidth/resolution, texture.getHeight()/resolution);        
        float xStart = 0;
        float xEnd = 1.0f/textureAngles;
        m.setBuffer(Type.TexCoord, 2, new float[]{xStart, 0,
                                                        xEnd, 0,
                                                        xEnd, 1,
                                                        xStart, 1});               
        Geometry geo = new Geometry("billboard", m);     
        Material mat = createMaterial("RotationTextures/" + name + ".png", assetManager);
        geo.setMaterial(mat);
        geo.updateGeometricState();
        geo.addControl(new RotationTextureBillboardControl(textureAngles));        
        return geo;
    }        
    
    private static Material createMaterial(String name,AssetManager assetManager){
        Material mat = null;        
        mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");                                         
        Texture texture = assetManager.loadTexture("Textures/" + name);
        mat.setTexture("ColorMap", texture);
        mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);        
        mat.getAdditionalRenderState().setDepthWrite(false);                 
        return mat;
    }
}
